﻿using System.Linq;
using LoungeRepo.EntityCache;
using NUnit.Framework;

namespace Test.LoungeRepo.EntityCache
{
    [TestFixture]
    public class testEntityCache
    {
        [Test]
        public void Register_entity_with_idempotence()
        {
            var sut = new SecondLevelCache();
            sut.Register("hello", "1", "p");
            sut.Register("hello", "1", "p");
        }


        [Test]
        public void Retrieve_empty_list_if_no_entity_has_been_registered_for_type()
        {
            var sut = new SecondLevelCache();
            Assert.AreEqual(0, sut.GetByType<string>().Count());
        }

        
        [Test]
        public void Retrieve_entities_of_a_given_type()
        {
            var sut = new SecondLevelCache();
            sut.Register("hello", "1", "p");

            Assert.AreEqual(1, sut.GetByType<string>().Count());

            sut.Register("world", "2", "p");
            sut.Register(99, "3", "q");
            Assert.AreEqual(2, sut.GetByType<string>().Count());
            Assert.AreEqual(1, sut.GetByType<int>().Count());
        }


        [Test]
        public void Delete_entity_from_cache()
        {
            var sut = new SecondLevelCache();

            sut.Register("hello", "1", "p");

            sut.Evict("1", "p");
            Assert.AreEqual(0, sut.GetByType<string>().Count());
        }


        [Test]
        public void Get_entity()
        {
            var sut = new SecondLevelCache();
            sut.Register("world", "2", "p");
            sut.Register("hello", "1", "p");

            object entity;
            Assert.IsTrue(sut.TryGetByKey("1", "p", out entity));
            Assert.AreEqual("hello", entity);
        }
    }
}
